package com.lineage.server.serverpackets;

import com.lineage.server.datatables.sql.CharacterTable;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.L1Clan;
import com.lineage.server.model.L1ClanMatching;
import com.lineage.server.model.L1War;
import com.lineage.server.world.World;
import com.lineage.server.world.WorldClan;
import com.lineage.server.world.WorldWar;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class S_ClanMatching extends ServerBasePacket {
  private byte[] _byte;
  
  private static final Log _log = LogFactory.getLog(S_ClanMatching.class);
  
  public S_ClanMatching(boolean postStatus, String clanname) {
    this._byte = null;
    writeC(0);
    writeC(postStatus ? 0 : 1);
    writeC(0);
    writeD(0);
    writeC(0);
  }
  
  public S_ClanMatching(L1PcInstance pc, int type, int objid, int htype) {
    this._byte = null;
    try {
      L1Clan clan = null;
      L1ClanMatching cml = L1ClanMatching.getInstance();
      String clanname = null;
      String text = null;
      writeC(0);
      writeC(type);
      if (type == 2) {
        ArrayList<L1ClanMatching.ClanMatchingList> showcmalist = new ArrayList<>();
        int i = 0;
        while (i < cml.getMatchingList().size()) {
          clanname = ((L1ClanMatching.ClanMatchingList)cml.getMatchingList().get(i))._clanname;
          if (!pc.getCMAList().contains(clanname))
            showcmalist.add(cml.getMatchingList().get(i)); 
          i++;
        } 
        int type2 = 0;
        int size = showcmalist.size();
        writeC(0);
        writeC(size);
        int j = 0;
        while (j < size) {
          text = ((L1ClanMatching.ClanMatchingList)showcmalist.get(j))._text;
          type2 = ((L1ClanMatching.ClanMatchingList)showcmalist.get(j))._type;
          String clan_name = ((L1ClanMatching.ClanMatchingList)showcmalist.get(j))._clanname;
          clan = WorldClan.get().getClan(clan_name);
          writeD(clan.getClanId());
          writeS(clan.getClanName());
          writeS(clan.getLeaderName());
          writeD(clan.getOnlineMaxUser());
          writeC(type2);
          if (clan.getHouseId() != 0) {
            writeC(1);
          } else {
            writeC(0);
          } 
          boolean inWar = false;
          List<L1War> warList = WorldWar.get().getWarList();
          Iterator<L1War> iterator = warList.iterator();
          while (iterator.hasNext()) {
            L1War war = iterator.next();
            if (war.checkClanInWar(clanname)) {
              inWar = true;
              break;
            } 
          } 
          if (inWar) {
            writeC(1);
          } else {
            writeC(0);
          } 
          writeC(0);
          writeS(text);
          writeD(clan.getEmblemId());
          j++;
        } 
        showcmalist.clear();
        showcmalist = null;
      } else if (type == 3) {
        int size2 = pc.getCMAList().size();
        writeC(0);
        writeC(size2);
        int i = 0;
        while (i < size2) {
          clanname = pc.getCMAList().get(i);
          clan = WorldClan.get().getClan(clanname);
          writeD(clan.getClanId());
          writeC(0);
          writeD(clan.getClanId());
          writeS(clan.getClanName());
          writeS(clan.getLeaderName());
          writeD(clan.getOnlineMaxUser());
          writeC((cml.getClanMatchingList(clanname))._type);
          if (clan.getHouseId() != 0) {
            writeC(1);
          } else {
            writeC(0);
          } 
          boolean inWar2 = false;
          List<L1War> warList2 = WorldWar.get().getWarList();
          Iterator<L1War> iterator2 = warList2.iterator();
          while (iterator2.hasNext()) {
            L1War war2 = iterator2.next();
            if (war2.checkClanInWar(clanname)) {
              inWar2 = true;
              break;
            } 
          } 
          if (inWar2) {
            writeC(1);
          } else {
            writeC(0);
          } 
          writeC(0);
          writeS((cml.getClanMatchingList(clanname))._text);
          writeD(clan.getEmblemId());
          i++;
        } 
      } else if (type == 4) {
        if (!cml.isClanMatchingList(pc.getClanname())) {
          writeC(130);
        } else {
          int size2 = pc.getInviteList().size();
          String username = null;
          writeC(0);
          writeC(2);
          writeC(0);
          writeC(size2);
          L1PcInstance user = null;
          int j = 0;
          while (j < size2) {
            username = pc.getInviteList().get(j);
            user = World.get().getPlayer(username);
            if (user == null)
              try {
                user = CharacterTable.get().restoreCharacter(username);
                if (user == null)
                  return; 
              } catch (Exception e) {
                e.printStackTrace();
              }  
            writeD(user.getId());
            writeC(0);
            writeC(user.getOnlineStatus());
            writeS(username);
            writeC(user.getType());
            writeH(user.getLawful());
            writeC(user.getLevel());
            writeC(1);
            j++;
          } 
        } 
      } else if (type == 5 || type == 6) {
        writeC(0);
        writeD(objid);
        writeC(htype);
      } 
    } catch (Exception e2) {
      _log.error(e2.getLocalizedMessage(), e2);
    } 
  }
  
  public byte[] getContent() {
    if (this._byte == null)
      this._byte = getBytes(); 
    return this._byte;
  }
  
  public String getType() {
    return getClass().getSimpleName();
  }
}
